﻿using System.Drawing;
using System.Windows.Forms;

namespace OmronEdit
{
    public partial class OmronEdit : TextBox
    {
        //private int minErr, memAddr;

        #region TTypeContent enum

        public enum TTypeContent {NumInt,NumFloat,Automan,OnOff,Mode}

        #endregion

        private bool _nonDigitEntered;

        public OmronEdit()
        {
            InitializeComponent();
            
        }

        public int MinError { get; set; }
        public int MaxError { get; set; }
        public float Step { get; set; }
        public TTypeContent TypeContent { get; set; }
        public OmronEdit FieldUp { get; set; }
        public OmronEdit FieldDown { get; set; }


        private void OmronEditKeyPress(object sender, KeyPressEventArgs e)
        {
            /*
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '-' && e.KeyChar!='.' )
                e.Handled = true;
            
            if (SelectionStart != 0 && e.KeyChar == '-')
                e.Handled = true;
           s
            */
            if (e.KeyChar == ',')
            {
                e.Handled = true;
                if (Text.IndexOf('.') < 0 && SelectionStart != 0)
                {
                    Text += ".";
                    SelectionStart = Text.Length;
                }
            }
            if ((Text.IndexOf('.') >= 0 || SelectionStart == 0) && e.KeyChar == '.')
                e.Handled = true;
            if (SelectionStart != 0 && e.KeyChar == '-')
                e.Handled = true;
            if (_nonDigitEntered)
                e.Handled = true;
        }

        private void OmronEditKeyDown(object sender, KeyEventArgs e)
        {
            _nonDigitEntered = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                        && e.KeyCode != Keys.Oemcomma && e.KeyCode != Keys.OemMinus
                        && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Subtract)
                        _nonDigitEntered = true;
                }
            }
        }
    }
}